package tyk.Service;

import tyk.Dao.BaseDao;
import tyk.pojo.Book;
import tyk.pojo.Merchant;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title BookService
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/11 15:22
 **/
public class BookService extends BaseDao {
    public int add(String name, BigDecimal price,String author,int sales,int stock,Integer merchant_id,String img_path) throws SQLException {
        String sql="insert into book (name,price,author,sales,stock,merchant_id,img_path) values (?,?,?,?,?,?,?);";
        int rows=super.executeUpdate(sql,name,price,author,sales,stock,merchant_id,img_path);
        System.out.println(img_path);
        return rows;
    }
    public List SelectEvery(){
        List<Book> list=new ArrayList<>();
        String sql="select id,name,price,author,sales,stock from book;";
        try {
            list = super.executeQuery(Book.class,sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }
    public List SelectEvery(Integer merchant_id){
        List<Book> list=new ArrayList<>();
        String sql="select id,name,price,author,sales,stock from book where merchant_id = ?;";
        try {
            list = super.executeQuery(Book.class,sql,merchant_id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }
    public List PageByPageNo(Integer pageNo,Integer ... params){
        Integer cnt;
        if (params.length==0) cnt=(pageNo-1)*12;
        else cnt=(pageNo-1)*10;
        List<Book> list=new ArrayList<>();
        String sql="select id,name,price,author,sales,stock,merchant_id,img_path from book limit ?,?;";
        try {
            if (params.length==0)
                list = super.executeQuery(Book.class,sql,cnt,12);
            else
                list = super.executeQuery(Book.class,sql,cnt,10);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }
    public List PageByPageNoMerchant_id(Integer pageNo,Integer merchant_id){
        Integer cnt;
        cnt=(pageNo-1)*12;
        List<Book> list=new ArrayList<>();
        String sql="select id,name,price,author,sales,stock,merchant_id from book where merchant_id = ? limit ?,?;";
        try {
            list = super.executeQuery(Book.class,sql,merchant_id,cnt,12);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }
    public void DeleteById(int id){
        String sql="delete from book where id = ?;";
        try {
            super.executeUpdate(sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public List SelectById(int id){
        String sql="select id,name,price,author,sales,stock,merchant_id from book where id = ?;";
        List<Book> list=new ArrayList<>();
        try {
            list=super.executeQuery(Book.class,sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return list;
    }
    public void EditBook(Book book){
        String sql="update book set name = ?,author= ?,price = ?,sales = ?,stock = ? where id = ?;";
        try {
            super.executeUpdate(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public Integer SelectMaxcount(Integer merchant_id) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Integer maxcount=0;
        List<Merchant> list=new ArrayList<>();
        String sql="select user_id,user_name,cnt from merchant where user_id = ?;";
        list=super.executeQuery(Merchant.class,sql,merchant_id);
        if(list.size()>0) maxcount=list.get(0).getCnt();
        return maxcount;
    }
    public List<Book> SelectBookBySales() throws SQLException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        List<Book>list;
        String sql="select id,name,price,author,sales,stock,img_path,merchant_id from book order by sales desc limit 10;";
        list=super.executeQuery(Book.class,sql);
        return list;
    }
}
